package com.shengde.jinwawaforleader.model.myJob.monitoring;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProVideoShowClickGoodCancle;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 设置摄像头的名称
 * Created by Administrator on 2016/10/24 0024.
 */
public class CameraNameActivity extends BaseActivity {
	private static final String TAG = "CameraNameActivity";
	private TextView tv_back;
	private TextView tv_title;
	private TextView fill_add;
	private EditText edt_setting_carmera_name;
	private String deviceName; //摄像头名称
	private String deviceSerial;//摄像头序列号

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.camera_name_activity);
	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		fill_add = (TextView) findViewById(R.id.fill_add);
		edt_setting_carmera_name = (EditText) findViewById(R.id.edt_setting_carmera_name);
	}

	@Override
	public void initData() {
		tv_title.setText(getString(R.string.monitoring_name));
		fill_add.setText(getString(R.string.save));

		deviceSerial = getIntent().getStringExtra("deviceSerial");
		Log.i(TAG, "initData: deviceSerial" + deviceSerial);
	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		fill_add.setOnClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
			case R.id.fill_add:
				showSaveSuccess();
				finish();
				break;
		}
	}

	/**
	 * 保存后 修改摄像头名称
	 */
	private void showSaveSuccess() {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/camera/cameraName";
		} else {
			url = BaseProtocol.HOST_BASE + "/camera/cameraName";
		}
		deviceName = edt_setting_carmera_name.getText().toString();
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("deviceSerial",deviceSerial);
		params.addParameter("deviceName",deviceName);
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProVideoShowClickGoodCancle pro_hot_news_list = new Gson().fromJson(result, ProVideoShowClickGoodCancle.class);
				if (pro_hot_news_list.getCode() ==0){
					Toast.makeText(CameraNameActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}
}
